"""
应用配置模块
使用 pydantic-settings 管理环境变量
"""
from typing import List
from pydantic_settings import BaseSettings
from pydantic import Field


class Settings(BaseSettings):
    """应用配置类"""
    
    # 应用基本信息
    APP_NAME: str = "医疗病历质量评估系统"
    APP_VERSION: str = "1.0.0"
    DEBUG: bool = False
    ENVIRONMENT: str = "production"
    
    # 服务器配置
    HOST: str = "0.0.0.0"
    PORT: int = 8000
    
    # 数据库配置
    DATABASE_URL: str = Field(
        default="postgresql+asyncpg://postgres:123456@192.168.166.199:5432/medical_qa",
        description="异步数据库连接URL"
    )
    DATABASE_URL_SYNC: str = Field(
        default="postgresql://postgres:123456@192.168.166.199:5432/medical_qa",
        description="同步数据库连接URL（用于Alembic）"
    )
    
    # 数据库连接池配置
    DB_POOL_SIZE: int = 20
    DB_MAX_OVERFLOW: int = 0
    DB_POOL_RECYCLE: int = 3600
    DB_ECHO: bool = False  # 生产环境关闭SQL日志
    
    # Redis配置
    REDIS_URL: str = "redis://localhost:6379/0"
    REDIS_ENABLED: bool = False
    
    # JWT认证配置
    SECRET_KEY: str = "your-secret-key-change-this-in-production"
    ALGORITHM: str = "HS256" 
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
    
    # # OpenAI配置
    # OPENAI_API_KEY: str = ""
    # OPENAI_MODEL: str = "gpt-4"
    # OPENAI_BASE_URL: str = "https://api.openai.com/v1"
    # OPENAI_TIMEOUT: int = 60
    # OPENAI_MAX_RETRIES: int = 3 
    
    # 本地LLM配置 - RTX 4060 (8GB VRAM) 优化
    USE_LOCAL_LLM: bool = True  # 启用本地LLM
    
    # 🚫 BioGPT 已弃用（不适用于中文病历）
    USE_BIOGPT: bool = False  # 已弃用，改用 HuaTuoGPT
    
    # 🧬 HuaTuoGPT 配置（中文医疗专业模型）
    USE_HUATUOGPT: bool = True  # 启用 HuaTuoGPT（专为中文医疗设计）
    HUATUOGPT_MODEL_NAME: str = "FreedomIntelligence/HuatuoGPT-II-7B"  # 模型名称
    HUATUOGPT_MAX_LENGTH: int = 2048  # 最大生成长度
    HUATUOGPT_TEMPERATURE: float = 0.2  # 医疗评估要求输出稳定
    HUATUOGPT_TOP_P: float = 0.9  # 核采样
    HUATUOGPT_TOP_K: int = 50  # Top-K 采样
    HUATUOGPT_TIMEOUT: int = 120  # 生成超时（秒）
    
    # Ollama 配置（Qwen 用于结构化检查）
    LOCAL_LLM_BASE_URL: str = "http://localhost:11434"
    QWEN_MODEL: str = "qwen3:8b"  # Qwen 模型（结构化数据检查）
    QWEN_TIMEOUT: int = 180
    QWEN_TEMPERATURE: float = 0.2
    QWEN_NUM_CTX: int = 8192
    
    # 回退配置
    FALLBACK_LLM_MODEL: str = "qwen2:7b"  # 第二回退模型
    FALLBACK_LLM_TIMEOUT: int = 180
    FALLBACK_LLM_NUM_CTX: int = 8192
    
    # RTX 4060 GPU加速配置
    LOCAL_LLM_NUM_GPU: int = 1  # 启用GPU
    LOCAL_LLM_NUM_THREADS: int = 8  # CPU线程数
    
    # 自动回退开关（HuaTuoGPT → Qwen → 规则引擎）
    ENABLE_MODEL_FALLBACK: bool = True
    
    # CORS配置
    CORS_ORIGINS: List[str] = [
        "http://localhost:5173",
        "http://localhost:5174",  # Vite 开发服务器端口
        "http://localhost:3000",
        "http://192.168.*.*:5173",
        "http://192.168.*.*:5174",
        "http://192.168.*.*:3000"

    ]
    CORS_ALLOW_CREDENTIALS: bool = True
    CORS_ALLOW_METHODS: List[str] = ["*"]
    CORS_ALLOW_HEADERS: List[str] = ["*"]
    
    # 日志配置
    LOG_LEVEL: str = "INFO"
    LOG_FILE: str = "logs/app.log"
    LOG_ROTATION: str = "1 day"
    LOG_RETENTION: str = "30 days"
    
    # 文件上传配置
    MAX_UPLOAD_SIZE: int = 10 * 1024 * 1024  # 10MB
    ALLOWED_EXTENSIONS: List[str] = [".json"]
    UPLOAD_DIR: str = "uploads"
    
    # API配置
    API_V1_PREFIX: str = "/api/v1"
    
    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"
        case_sensitive = True


# 创建全局配置实例
settings = Settings()

